<template>
  <div id="BarWithBackground"
       :style="{height: height, width: width}"></div>
</template>

<script>
import echarts from 'echarts'

export default {
  name: "BarWithBackground",
  props: {
    xData: {
      type: Array,
      required: true
    },
    seriesData: {
      type: Array,
      required: true
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '400px'
    },
    height: {
      type: String,
      default: '400px'
    },
    backgroundStyle: {
      type: String,
      default: 'rgba(180, 180, 180, 0.2)'
    },
    color: {
      type: String,
      default: '#5470c6'
    }
  },
  mounted() {
    this.graph = echarts.init(document.getElementById('BarWithBackground'))
    this.graph.clear()
    this.graph.setOption(this.option)
  },
  watch: {
    seriesData() {
      this.graph.clear()
      this.graph.setOption(this.option)
    }
  },
  data() {
    return {
      graph: null,
      option: {
        title: {
          text: this.title
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.xData,  // 如：['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          axisTick: {
            alignWithLabel: true
          }
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.seriesData,   // 如：[120, 200, 150, 80, 70, 110, 130]
          type: 'bar',
          showBackground: true,
          color: this.color,
          backgroundStyle: {
            color: this.backgroundStyle
          },
          animationDuration: 2800,
          animationEasing: "cubicInOut"
        }]
      }
    }
  }
}
</script>

<style scoped>
</style>